import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const Login = () => import('../components/common/login/Login')

const Users = () => import('../components/content/users/Users')
const Cate = () => import('../components/content/good/Cate')
const Params = () => import('../components/content/params/Params')
const GoodsList = () => import('../components/content/goodslist/goodsList')
const Add = () => import('../components/content/add/Add')
const Order = () => import('../components/content/order/Order')
const Reports = () => import('../components/content/reports/Reports')

const Home = () => import('../views/home/Home')
const WelCome = () => import('../views/home/childComps/welCome')
const Rights = () => import('../views/home/childComps/Rights')
const Roles = () => import('../views/home/childComps/Roles')

  const routes = [
		{
			path:"/",
			redirect:'/login'
		},
		{
			path:'/login',
			component:Login
		},
		{
			path:'/home',
			component:Home,
			redirect:'/welcome',
			children:[
				{
					path:'/welcome',
					component:WelCome
				},
				{
					path:'/users',
					component:Users
				},
				{
					path:'/rights',
					component:Rights
				},
				{
					path:'/roles',
					component:Roles
				},
				{
					path:'/categories',
					component:Cate
				},
				{
					path:'/params',
					component:Params
				},
				{
					path:'/goods',
					component:GoodsList
				},
				{
					path:'/goods/add',
					component:Add
				},
				{
					path:"/orders",
					component:Order
				},
				{
					path:'/reports',
					component:Reports
				}
			]
		},
		
]

const router = new VueRouter({
  routes,
	mode:'history'
	
})

//设置路由守卫
router.beforeEach((to,from,next) => {
	//to   将要访问的路径
	//from  代表从那一个路径跳转而来
	//next  是一个函数，表示放行
	// next() 放行   next('/login') 强制跳转页面
	if(to.path === '/login') return next()
	//获取token
	const tokenStr = window.sessionStorage.getItem("token")
	if(!tokenStr) return next('/login')
	next()
})


export default router
